package com.sunny.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Sunny on 2016/4/15.
 */

public class RegexUtil {
    public RegexUtil() {
    }

    public static ArrayList<String[]> getListMatcher(String str, String regex) {
        ArrayList list = new ArrayList();
        Pattern pattern = Pattern.compile(regex, 2);
        Matcher matcher = pattern.matcher(str);

        while(matcher.find()) {
            int i = matcher.groupCount();
            String[] s = new String[i];

            for(int j = 1; j < i + 1; ++j) {
                s[j - 1] = matcher.group(j);
            }

            list.add(s);
        }

        return list;
    }

    public static String replaceMatcher(String str, String regex, String tostr) {
        Pattern pattern = Pattern.compile(regex, 2);
        Matcher matcher = pattern.matcher(str);
        StringBuffer buf = new StringBuffer();

        while(matcher.find()) {
            matcher.appendReplacement(buf, tostr);
        }

        matcher.appendTail(buf);
        return buf.toString();
    }

    public static String getHiddenInputRegex() {
        return "<input[^>]+type=\"hidden\"[^>]+name=\"([^\"]+)\"[^>]+value=\"([^\"]*)\"[^>]*>";
    }

    public static String getEditorImgRegex() {
        return "<img[^>]+sid=[\'\"]([^\'\"]+)[\'\"][^>]*>";
    }

    public static Integer[] getEditorImgList(String str) {
        ArrayList list = getListMatcher(str, getEditorImgRegex());
        HashSet set = new HashSet();
        Iterator var4 = list.iterator();

        while(var4.hasNext()) {
            String[] aa = (String[])var4.next();
            set.add(Integer.valueOf(NumberUtil.parseInt(aa[0])));
        }

        return (Integer[])set.toArray(new Integer[0]);
    }

    public static boolean compare(String regex, String str) {
        Pattern pattern = Pattern.compile(regex, 2);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }

    public static boolean find(String regex, String str) {
        Pattern reg = Pattern.compile(regex);
        Matcher matcher = reg.matcher(str);
        return matcher.find();
    }

    public static String replaceBlankSpan(String str) {
        return replaceMatcher(str, "<SPAN style=\"FONT-SIZE: 0pt; COLOR: #ffffff\"[^>]*>[^<]*</SPAN>", "");
    }

    public static String toLink(String content) {
        content = replaceMatcher(content, "((?:(?:http|ftp|https):\\/\\/){0,1}(?:(?:\\w|\\d)+\\.)+(?:\\S+))", "<a href=\"$1\">$1</a>");
        return content;
    }

    public static String deleteHTML(String content) {
        try {
            String e = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>";
            String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>";
            String regEx_html = "<[^>]+>";
            Pattern p_script = Pattern.compile(e, 2);
            Matcher m_script = p_script.matcher(content);
            content = m_script.replaceAll("");
            Pattern p_style = Pattern.compile(regEx_style, 2);
            Matcher m_style = p_style.matcher(content);
            content = m_style.replaceAll("");
            Pattern p_html = Pattern.compile(regEx_html, 2);
            Matcher m_html = p_html.matcher(content);
            content = m_html.replaceAll("");
            p_html = Pattern.compile(" {2,}");
            m_html = p_html.matcher(content);
            content = m_html.replaceAll(" ");
            p_html = Pattern.compile("[\\t\\r\\n]{2,}");
            m_html = p_html.matcher(content);
            content = m_html.replaceAll("\n");
            return content;
        } catch (Exception var10) {
            return "";
        }
    }

    public static String getNumber(String s) {
        Matcher m = Pattern.compile("(\\d+\\.{1}\\d+)").matcher(s);
        m.find();
        return m.group();
    }
}

